package com.susallwave.srcb.controller.sys.rbac;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.susallwave.srcb.aspect.MyLog;
import com.susallwave.srcb.entity.sys.User;
import com.susallwave.srcb.response.Result;
import com.susallwave.srcb.service.sys.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author MyBatisPlusGenerator
 * @since 2025-07-19
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(tags = "用户管理", description = "用户登录、锁定、信息维护等操作")
public class UserController {

    private final UserService userService;

    // 用户登录
    @ApiOperation(value = "sa-token用户登录", notes = "通过银行用户ID和密码登录")
    @PostMapping("/login")
    public Result<String> login(@RequestParam String userAcct,
                                @RequestParam String password) {
        return userService.login(userAcct, password);
    }

    // 用户登出
    @ApiOperation(value = "sa-token用户登出", notes = "用户登出")
    @PostMapping("/logout")
    public Result<Void> logout() {
        return userService.logout();
    }

    // sign登录
    @ApiOperation(value = "sa-token之sign免密登录", notes = "通过链接传递sign签名和时间戳信信任登陆")
    @PostMapping("/loginBySign")
    public String loginByTokenUrl(@ApiParam(value = "签名",required = true) @RequestParam String sign,
                                  @ApiParam(value = "当前时间戳",required = true) @RequestParam String timestamp,
                                  @ApiParam(value = "跳转页码值(两系统商定的)",required = true) @RequestParam String pageCode,
                                  @ApiParam(value = "员工号",required = true) @RequestParam String userAcct,
                                  @ApiParam(value = "来访系统标志",required = true) @RequestParam String sysFlag) {

        return userService.loginBySign(sign,userAcct,timestamp,pageCode,sysFlag);

    }

    // 锁定用户
    @ApiOperation(value = "锁定用户", notes = "锁定用户多长时间,单位秒")
    @PostMapping("/lock")
    public Result<Void> lockUser(@RequestParam String userAcct,@RequestParam long timeout) {
        return userService.lockUser(userAcct,timeout);
    }

    // 解锁用户
    @ApiOperation(value = "解锁用户", notes = "解锁用户")
    @PostMapping("/unlock")
    public Result<Void> unlockUser(@RequestParam String userAcct) {
        return userService.unlockUser(userAcct);
    }

    // 获取用户信息
    @ApiOperation(value = "获取用户信息", notes = "查询指定用户的详细信息")
    @GetMapping("/single")
    public Result<User> single(@RequestParam String userAcct) {
        return userService.getUserInfo(userAcct);
    }

    // 获取用户列表
    @MyLog(enabled = false)
    @ApiOperation(value = "用户列表", notes = "用户列表")
    @GetMapping("/list")
    public Result<IPage<User>> userList(@RequestParam(defaultValue = "1") int pageNum,
                                        @RequestParam(defaultValue = "10") int pageSize,
                                        @RequestParam("keyword") String keyword) {
        return userService.userList(pageNum,pageSize,keyword);
    }

}
